// pages/cpc/me/me.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: null,
    card_list: [],
  },

  // 动态设定navigationBarTitleText
  change: function(title){ 
    wx.setNavigationBarTitle({
      title: title
    })
  },

  // 点击卡片，根据类型决定跳转到帖子还是活动
  onCardTap: function(e) {
    var id = e.currentTarget.dataset.id
    var type = this.data.type
    if(type==1){
      wx.navigateTo({
        url: '../postDetail/postDetail?id=' + id,
      })
    }else{
      wx.navigateTo({
        url: '../activityDetail/activityDetail?id=' + id,
      })
    }
  },

  onLoad: function (options) {
    var type = options.type
    this.setData({
      type: type
    })
    if(type==1){
      this.change("我的发帖")
      app.sendGetRequest('/community/cpc/post/getMyPost', (res) => {
        var data = res.data
        var post_list = []
        for(var i=0; i<data.length; i++){
          post_list.push({
            title: data[i].title,
            extra: data[i].commentNumber+"评论",
            id: data[i].postId,
            body: data[i].content,
            footer: "发布时间: "+data[i].postCreateTime,
          })
        }
        this.setData({
          card_list: post_list
        })
      }, (err) => {
        console.log(err.errMsg)
      })
    }else if(type==2){
      this.change("我报名的活动")
      app.sendGetRequest('/community/cpc/enroll/getMyEnroll', (res) => {
        var data = res.data
        var activity_list = []
        for(var i=0; i<data.length; i++){
          activity_list.push({
            title: data[i].title,
            extra: data[i].enrollNumber+"人已报名",
            id: data[i].activityId,
            body: data[i].content,
            footer: "活动时间: "+data[i].startTime,
          })
        }
        this.setData({
          card_list: activity_list
        })
      }, (err) => {
        console.log(err.errMsg)
      })
    }else if(type==3){
      this.change("我参与的活动")
      app.sendGetRequest('/community/cpc/participate/getMyParticipate', (res) => {
        var data = res.data
        var activity_list = []
        for(var i=0; i<data.length; i++){
          activity_list.push({
            title: data[i].title,
            extra: data[i].participationNumber+"人已签到",
            id: data[i].activityId,
            body: data[i].content,
            footer: "活动时间: "+data[i].startTime,
          })
        }
        this.setData({
          card_list: activity_list
        })
      }, (err) => {
        console.log(err.errMsg)
      })
    }else{
      console.log("error type")
    }
  },
})